package com.homedo.microservice.open.service.gateway.manager.web.remote.api.impl;

import com.google.common.collect.Lists;
import com.homedo.common.bean.data.RequestData;
import com.homedo.common.bean.data.ResponseData;
import com.homedo.common.bean.vo.Order;
import com.homedo.common.bean.vo.Pagination;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.manager.web.api.IConfigServiceRemote;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.ConfigAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.ConfigAddAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.ConfigPageAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.ConfigUpdateAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.ConfigAResp;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.ConfigPageAResp;
import com.homedo.microservice.open.service.gateway.manager.web.convent.CommonConvent;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.Config;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.ConfigEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.service.ConfigService;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.support.ApplicationNameHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigServiceRemoteImpl implements IConfigServiceRemote {
    @Autowired
    private ConfigService configService;


    @Override
    public ResponseData<ConfigPageAResp> list(@RequestBody RequestData<ConfigPageAReq> requestData) {
        ResponseData<ConfigPageAResp> responseData = new ResponseData<>();

        Pagination<ConfigEnhanced> pagination = new Pagination<>(1, Integer.MAX_VALUE, false);
        pagination.addOrder(new Order(Config.COLUMN_CREATE_TIME, Order.ORDER_DESC), Config.class);


        pagination = configService.getPagination4Application(ApplicationNameHolder.get(), Lists.newArrayList(requestData.getRequest().getZuulFilterId()), pagination, null);
        ConfigPageAResp configPageAResp = new ConfigPageAResp();
        configPageAResp.setCount(pagination.getCount());
        configPageAResp.setPage(pagination.getIndex());
        configPageAResp.setSize(pagination.getSize());

        if (Detect.notEmpty(pagination.getItems())) {
            configPageAResp.setItems(CommonConvent.convent2ConfigAResps(pagination.getItems()));
        }

        responseData.setData(configPageAResp);

        return responseData;
    }

    @Override
    public ResponseData<ConfigAResp> detail(@RequestBody RequestData<ConfigAReq> requestData) {
        ResponseData<ConfigAResp> responseData = new ResponseData<>();

        ConfigEnhanced configEnhanced = configService.getEnhanceds4Application(ApplicationNameHolder.get(), requestData.getRequest().getName(), null);
        if (configEnhanced != null) {
            responseData.setData(CommonConvent.convent2ConfigAResp(configEnhanced));
        }

        return responseData;
    }

    @Override
    public ResponseData update(@RequestBody RequestData<ConfigUpdateAReq> requestData) {
        ResponseData responseData = new ResponseData();

        ConfigUpdateAReq configUpdateAReq = requestData.getRequest();

        configService.updateConfig(configUpdateAReq.getApplicationName(), configUpdateAReq.getName(), configUpdateAReq.getValue(), configUpdateAReq.getComment(), null);

        return responseData;
    }

    @Override
    public ResponseData<ConfigAResp> add(@RequestBody RequestData<ConfigAddAReq> requestData) {
        ResponseData<ConfigAResp> responseData = new ResponseData<>();

        ConfigAddAReq configAddAReq = requestData.getRequest();

        Config config = new Config();
        BeanUtils.copyProperties(configAddAReq, config);

        config = configService.addConfig(config, configAddAReq.getZuulFilterId(), null);

        responseData.setData(CommonConvent.convent2ConfigAResp((ConfigEnhanced) configService.convent2Enhanced(config)));

        return responseData;
    }
}
